﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using University.Workshop.Model;
using System.Data;

[assembly: CLSCompliant(true)]
namespace University.Workshop.DataAccess
{
    public class OperatorRegister: IDisposable
    {
        const string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=DataBaseHotel;Integrated Security=True";
        SqlConnection conex;
        public bool ExistOperatorByUserName(string userName)
        {
            bool ok;
            conex = new SqlConnection(connectionString);
            SqlCommand cmd = conex.CreateCommand();
            cmd.CommandText = "SELECT [UserName] ,[Password] FROM [DataBaseHotel].[dbo].[Administrator] WHERE UserName=@UserName";
            cmd.Parameters.AddWithValue("@UserName", userName);
            conex.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.HasRows)
                ok = true;
            else
                ok = false;
            return ok;
        }
        public bool ExistOperatorByPassword(string password)
        {
            bool ok;
            conex = new SqlConnection(connectionString);
            SqlCommand cmd = conex.CreateCommand();
            cmd.CommandText = "SELECT [UserName] ,[Password] FROM [DataBaseHotel].[dbo].[Administrator] WHERE [Password]=@Password ";
            cmd.Parameters.AddWithValue("@Password", password);
            conex.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.HasRows)
                ok = true;
            else
                ok = false;
            return ok;
        }
        public void RegisterOperator(Administrator administrator)
        {
            if (administrator == null)
                throw new ArgumentException("Invalid Input Parameters");
            
            conex = new SqlConnection(connectionString);
            SqlCommand cmd = conex.CreateCommand();
            cmd.CommandText = @"INSERT INTO [DataBaseHotel].[dbo].[Administrator]
                                    ([UserName]
                                    ,[Password]
                                    ,[FullName]
                                    ,[Surname]
                                    ,[Email])
                                VALUES
                                    (@UserName
                                    ,@Password
                                    ,@FullName
                                    ,@Surname
                                    ,@Email)";
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.AddWithValue("@UserName", administrator.UserName);
            cmd.Parameters.AddWithValue("@Password", administrator.Password);
            cmd.Parameters.AddWithValue("@FullName", administrator.Name);
            cmd.Parameters.AddWithValue("@Surname", administrator.Surname);
            cmd.Parameters.AddWithValue("@Email", administrator.Email);
            
            try
            {
                conex.Open();
                cmd.ExecuteNonQuery();
                
            }
            catch (Exception e)
            {
                throw new ArgumentException("Error creating a user", e.Message);
            }
        }
        public int CannotRowsAdministrator()
        {
            int countRows = 0;
            conex = new SqlConnection(connectionString);
            SqlCommand cmd = conex.CreateCommand();
            cmd.CommandText = "SELECT * FROM [DataBaseHotel].[dbo].[Administrator]";
            cmd.CommandType = CommandType.Text;
            try
            {
                conex.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    countRows++;
                }
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            return countRows;
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing) 
        {
            if (disposing)
            {
                if (conex!=null)
                {
                    conex.Dispose();
                    conex = null;
                }  
            }
        }
    }
}
